package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * 对象序列化、反序列化
 * Created by Chilly Cui on 2020/9/24.
 */
public class ObjectDemo {
    @Test
    void test() throws IOException {
        User user = new User();
        user.setName("马超");

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/env/三国/马超.txt"));
        oos.writeObject(user);

        oos.flush();
    }

    @Test
    void test2() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("c:/env/三国/马超.txt")));
        // 强转为user
        User user = (User) ois.readObject();
        System.out.println(user);
        // 输出内容
        //User(name=马超, age=20)
    }
}
